
class Queue:
    """此队列类，主要由列表实现，是一个环状，所以可以达到比较好的效率"""
    def __init__(self, max_capacity=1024):
        self.max_capacity = max_capacity
        self.queue = [None] * max_capacity
        self.head = 0
        self.tail = 0
        self.flag = 0
    
    def put(self, value):
        if self.flag > self.max_capacity-1:     
            raise Exception('It is full')
        self.flag += 1
        self.queue[self.tail] = value    
        self.tail = self.tail + 1 if self.tail < self.max_capacity-1 else 0
        return self
        
    def get(self):
        if self.flag <= 0 and self.head == self.tail:
            raise Exception('Empty')
        ret = self.queue[self.head]
        self.queue[self.head] = None       
        self.head = self.head + 1 if self.head < self.max_capacity-1 else 0
        self.flag -= 1
        return ret
    
    def show(self):    
        if self.head < self.tail:
            print(self.queue[self.head:self.tail])
        elif self.flag == self.max_capacity:
            print(self.queue)
        elif self.head == self.tail:
            print(self.queue[self.head:self.tail])
        else:
            print(self.queue[self.head:]+self.queue[:self.tail])
    
    
q = Queue(5)
